from fastapi import APIRouter, Depends, Security
from fastapi.security import SecurityScopes

from app.api.response import Response
from app.api.status import Status
from app.middleware.auth import get_current_user
from app.api.exception import UnauthorizedError
from app.schemas.auth_type import AuthEnum

auth_router = APIRouter()


def check_user(scopes: SecurityScopes, user: dict = Depends(get_current_user)):
    if user.get("roles") not in scopes.scopes:
        raise UnauthorizedError(msg="权限不足")


@auth_router.get("/auth_admin",
                 dependencies=[Security(check_user, scopes=AuthEnum.admin.value)],
                 summary="管理员权限验证",
                 response_model=Response
                 )
async def auth_admin():
    return Response.success(data="auth_admin", status=Status.SUCCESS)


@auth_router.get("/auth_user",
                 dependencies=[Security(check_user, scopes=AuthEnum.user.value)],
                 summary="用户权限验证",
                 )
async def auth_user():
    return Response.success(data="auth_user", status=Status.SUCCESS)


# merchant 商户
@auth_router.get("/auth_merchant",
                 dependencies=[Security(check_user, scopes=AuthEnum.merchant.value)],
                 summary="商户权限验证",
                 )
async def auth_merchant():
    return Response.success(data="auth_merchant", status=Status.SUCCESS)


# agent 代理商
@auth_router.get("/auth_agent",
                 dependencies=[Security(check_user, scopes=AuthEnum.agent.value)],
                 summary="代理商权限验证",
                 )
async def auth_agent():
    return Response.success(data="auth_agent", status=Status.SUCCESS)
